import time

import numpy as np


from PIL import ImageGrab


# 两张图片相似度比较

class ImgUtil:
    def __init__(self):
        print('ok')


# 截图
def grapImage(path='') -> bytearray:
    img = ImageGrab.grab()
    if path: img.save(path)
    img = np.array(img.getdata(), np.uint8).reshape(img.size[1], img.size[0], 3)
    return img


# 图片相似度
def compareImg(img1, img2) -> float:
    s = 0
    if img1.shape == img2.shape:
        for i in range(img1.shape[0]):
            for j in range(img1.shape[1]):
                for k in range(img1.shape[2]):
                    if img1[i][j][k] != img2[i][j][k]:
                        s = s - 1
                        break
                s = s + 1
    return s / (img1.shape[0] * img1.shape[1])


# 一张图片是否包含另一张图片
def subImg(img1, img2, step=0) -> bool:
    for i in range(img1.shape[0] - img2.shape[0] + 1):
        for j in range(img1.shape[1] - img2.shape[1] + 1):
            if compareArr(img1[i][j], img2[0 + step][0 + step]):
                for x in range(img2.shape[0] - step):
                    for y in range(img2.shape[1] - step):
                        # print(i, j, x, y)
                        if not compareArr(img1[i + x][j + y], img2[x][y]):
                            x = img2.shape[0] - step
                            break
                if x == img2.shape[0] - step - 1 and y == img2.shape[1] - step - 1:
                    return True
    return False


# 比较数组
def compareArr(arr1, arr2):
    for i in range(0, len(arr1)):
        if arr1[i] != arr2[i]:
            break
        return True
    return False

